'use strict';
const Controller = require('egg').Controller;
// import requests;
class LoginController extends Controller {
    async post() {
        const {
            ctx,
            app
        } = this;
        const {
            code
        } = ctx.request.body;
        console.log("code  淡淡的");
        console.log(code);
        if (code) {
            const appid = 'wxa6ef6ccf6ef51b2c',
                secret = '1e24d874efe6bf8769f8e64aed9b95ee';
            const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`;
            // egg.js中请求外部接口，this.ctx.curl()
            const res = await ctx.curl(url, {
                // method: 'GET',// 请求方法
                dataType: 'json',// 字符串-响应数据的类型。可能是text或json
            });
            if (res.data.openid) {
                
                const a = res.data.openid;
                const iner = {
                    log_time: Date.now(),
                    end_time: Date.now() + 3600 * 2
                };
                const token = app.jwt.sign(iner, app.config.jwt.sercet);
                ctx.body = {
                    code: 0,
                    msg: '登录成功',
                    data: a
                }
            } else {
                ctx.body = {
                    code: 4004,
                    msg: '登录失败'
                }
            }
        } else {
            ctx.body = {
                code: "4006",
                msg: "缺少必要参数！！"
            }
        }
    }
}
module.exports = LoginController;
